package cn.edu.xaut.mybatisers.mall.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitConfig {

    @Bean
    public Queue billDelayQueue(){
        // 消息队列的名字: TestDirestQueue
        // durable:是否持久化,默认是false,持久化队列：会被存储在磁盘上，当消息代理重启时仍然存在，暂存队列：当前连接有效
        return new Queue("billDelayQueue",true);
    }

    @Bean
    public Queue hotCommodityDelayQueue(){
        return new Queue("hotCommodityDelayQueue", true);
    }

    @Bean
    CustomExchange delayExchange(){
        // 交换机名字: TestDirectExchange
        Map<String, Object> args =  new HashMap<>();
        args.put("x-delayed-type", "direct");
        return new CustomExchange("customerExchange", "x-delayed-message",true,false, args);
    }

    @Bean
    Binding bindingBillQueue(){
        // 绑定交换机与队列
        return BindingBuilder.bind(billDelayQueue()).to((delayExchange())).with("billDelayQueue").noargs();
    }

    @Bean
    Binding bindingCommodityQueue(){
        // 绑定交换机与队列
        return BindingBuilder.bind(hotCommodityDelayQueue()).to((delayExchange())).with("hotCommodityDelayQueue").noargs();
    }
}
